package cn.nuc.experiment.controller;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.nuc.experiment.entity.ExperimentEntity;
import cn.nuc.experiment.service.ExperimentService;
import cn.nuc.common.utils.PageUtils;
import cn.nuc.common.utils.R;


@RestController
@RequestMapping("experiment")
public class ExperimentController {
  @Autowired
  @Qualifier("redisTemplate")
  private RedisTemplate redisTemplate;
  @Autowired
  private ExperimentService experimentService;

  private long nextExperimentId;

  //redis 实验列表初始化
  @RequestMapping("/init")
  public R init() {
    // 第一次获取列表从mysql数据库中取出存入redis数据库中
    List<ExperimentEntity> list = experimentService.list();
    Iterator iterator = list.iterator();
    for (; iterator.hasNext(); ) {
      ExperimentEntity experiment = (ExperimentEntity) iterator.next();
      String t = "experiment" + experiment.getEid();
      redisTemplate.opsForValue().set(t, experiment);
      redisTemplate.opsForList().rightPush("experiments", t);

    }
    nextExperimentId = redisTemplate.opsForList().size("experiments");
    return R.ok();
  }
  /**
   * 列表
   */
  @RequestMapping("/list")
  //@RequiresPermissions("experiment:experiment:list")
  public R list(@RequestParam Map<String, Object> params) {
    List experimentsS = redisTemplate.opsForList().range("experiments", 0, redisTemplate.opsForList().size("experiments"));
    List<ExperimentEntity> experimentFirstList = redisTemplate.opsForValue().multiGet(experimentsS);
    return R.ok().put("experiment", experimentFirstList);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{eid}")
  //@RequiresPermissions("experiment:experiment:info")
  public R info(@PathVariable("eid") Integer eid) {
//    ExperimentEntity experiment = experimentService.getById(eid);
    ExperimentEntity experiment = (ExperimentEntity) redisTemplate.opsForValue().get("experiment"+eid);
    return R.ok().put("experiment", experiment);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  //@RequiresPermissions("experiment:experiment:save")
  public R save(@RequestBody ExperimentEntity experiment) {
    experimentService.save(experiment);
    redisTemplate.opsForValue().set("experiment"+nextExperimentId++,experiment);
    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  //@RequiresPermissions("experiment:experiment:update")
  public R update(@RequestBody ExperimentEntity experiment) {
    experimentService.updateById(experiment);
    redisTemplate.opsForValue().set("experiment"+experiment.getEid(),experiment);
    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete/{eid}")
  //@RequiresPermissions("experiment:experiment:delete")
  public R delete(@PathVariable("eid") Integer eid) {
    experimentService.removeById(eid);
    redisTemplate.opsForValue().set("experiment"+eid,"del",1, TimeUnit.SECONDS);
    redisTemplate.opsForList().remove("experiments",1,"experiment"+eid);
    return R.ok();
  }

}
